﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class DepositoryController : GenericController {
        //
        // GET: /Depository/
        //库存检查，采购用
        public ActionResult CheckDepository() {
            var enums = unitOfWork.DepositoryRepository.dbSet.ToList()
                .Where(d => d.SafetyCount > d.Total);
            if (enums.Count() < 1) {
                return Json2(new { success = true });
            } else {
                var jsonData = enums.Select(d => new { msg = "库存：" + d.ProductValueObject.Number + " " + d.ProductValueObject.Name + "的数量为：" + d.Total + "，小于安全库存需要的" + d.SafetyCount + d.ProductValueObject.Unit });
                return Json2(new { success = false, messages = jsonData});
            }
        }
        [HttpPost]
        public ActionResult PMAdd(int orderItemId, DateTime date, double count, string number, string location, string remark) {
            var orderItem = unitOfWork.OrderItemRepository.Get(orderItemId);
            string n = orderItem.ProductValueObject.Number;
            var partsDepository = unitOfWork.PartsRepository.GetByNumber(n);
            if (partsDepository != null) {
                PartsInDepository(orderItemId, date, count, number, location, remark);
            }
            var semifinishedDepository = unitOfWork.SemifinishedRepository.GetByNumber(n);
            if (semifinishedDepository != null) {
                SemifinishedInDepository(orderItemId, date, count, number, location, remark);
            }
            return Json2(new { success = true });
        }
        //number批次号
        private void PartsInDepository(int orderItemId, DateTime date, double count, string number, string location, string remark) {
            var orderItem = unitOfWork.OrderItemRepository.Get(orderItemId);
            if (orderItem != null) {
                string nu = orderItem.ProductValueObject.Number;
                //找库存
                var partsDepository = unitOfWork.PartsDepositoryRepository.Get(nu);
                //库存不存在
                if (partsDepository == null) {
                    partsDepository = new PartsDepository {
                        ProductValueObject = orderItem.ProductValueObject, Remark = remark,
                        DepositoryItems = new List<DepositoryItem>()
                    };
                    unitOfWork.PartsDepositoryRepository.Add(partsDepository);
                }
                //库存项目存在，直接加数量。不存在，库存加一个项
                var depositoryItem = partsDepository.DepositoryItems.Where(d => d.Number == number).FirstOrDefault();
                if (depositoryItem != null) {
                    depositoryItem.Count += count;
                } else {
                    depositoryItem = new DepositoryItem {
                        Number = number, Count = count, Location = location
                    };
                    partsDepository.DepositoryItems.Add(depositoryItem);
                }

                //插入库记录
                unitOfWork.PartsInDepositoryRepository.Add(new PartsInDepository {
                    Count = count, Location = location, Date = date, Number = number,
                    OrderItemId = orderItem.OrderItemId, ProductValueObject = orderItem.ProductValueObject
                });

                //增加订单项的入库数量属性
                orderItem.InDepositoryCount += count;

                unitOfWork.Save();
            }
        }

        private void SemifinishedInDepository(int orderItemId, DateTime date, double count, string number, string location, string remark) {
            var orderItem = unitOfWork.OrderItemRepository.Get(orderItemId);
            if (orderItem != null) {
                string nu = orderItem.ProductValueObject.Number;
                //找库存
                var semifinishedDepository = unitOfWork.SemifinishedDepositoryRepository.Get(nu);
                //库存不存在
                if (semifinishedDepository == null) {
                    semifinishedDepository = new SemifinishedDepository {
                        ProductValueObject = orderItem.ProductValueObject, Remark = remark,
                        DepositoryItems = new List<DepositoryItem>()
                    };
                    unitOfWork.SemifinishedDepositoryRepository.Add(semifinishedDepository);
                }
                //库存项目存在，直接加数量。不存在，库存加一个项
                var depositoryItem = semifinishedDepository.DepositoryItems.Where(d => d.Number == number).FirstOrDefault();
                if (depositoryItem != null) {
                    depositoryItem.Count += count;
                } else {
                    depositoryItem = new DepositoryItem {
                        Number = number, Count = count, Location = location
                    };
                    semifinishedDepository.DepositoryItems.Add(depositoryItem);
                }

                //插入库记录
                unitOfWork.SemifinishedInDepositoryRepository.Add(new SemifinishedInDepository {
                    Count = count, Location = location, Date = date, Number = number,
                    OrderItemId = orderItem.OrderItemId, ProductValueObject = orderItem.ProductValueObject
                });

                //增加订单项的入库数量属性
                orderItem.InDepositoryCount += count;

                unitOfWork.Save();
            }
        }
    }
}
